<template>
  <!-- 导入运输 -->
  <el-dialog :title="currentSelect + '导入'" :visible.sync="visible" width="800px" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item :label="currentSelect + '名称'" prop="equipmentName">
        <el-input
          v-model="queryParams.equipmentName"
          :placeholder="'请输入' + currentSelect + '名称'"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排放因子" prop="carbonFactor">
        <el-input
          v-model="queryParams.carbonFactor"
          placeholder="请输入排放因子"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table @row-click="clickRow" ref="table" :data="dbTableList" @selection-change="handleSelectionChange" height="260px">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="equipmentId" :label="currentSelect + 'ID'"></el-table-column>
        <el-table-column prop="equipmentName" :label="currentSelect + '名称'" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="unit" label="单位" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="carbonFactor" label="排放因子" :show-overflow-tooltip="true"></el-table-column>
<!--        <el-table-column prop="createTime" label="创建时间"></el-table-column>-->
<!--        <el-table-column prop="updateTime" label="更新时间"></el-table-column>-->
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :disabled="single" @click="handleImportTable">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listEquipmentConfig } from "@/api/business/equipmentConfig"

import { addMasterialTransport } from "@/api/business/masterialTransport"

export default {
  props: {
    engineeringId: {
      required: false,
      type: Number
    },
    usagePhase: {
      required: true,
      type: String
    },
  },
  data() {
    return {
      // 遮罩层
      visible: false,
      // 选中数组值
      values: [],
      // 非单个禁用
      single: true,
      // 总条数
      total: 0,
      // 表数据
      dbTableList: [],
      currentSelect: "材料",
      currentIndex: 0,
      selectTypeData: ["材料", "设备"],
      selectFormData: ["建材列表", "运输列表"],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentName: null,
        equipmentType: null,
        equipmentForm: null,
      },
      // 选中的建材名称
      materialName: null,
      // 建材的碳排放因子
      carbonFactor: null,
      // 建材的单位
      unit: null,
      importData: []
    }
  },
  methods: {
    // 显示弹框
    show() {
      this.getList()
      this.visible = true
    },
    clickRow(row) {
      this.$refs.table.toggleRowSelection(row)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.values = selection
      this.single = selection.length!==1
    },
    // 查询表数据
    getList() {
      this.queryParams.equipmentType = this.selectTypeData[this.currentIndex]
      this.queryParams.equipmentForm = this.selectFormData[this.currentIndex]
      listEquipmentConfig(this.queryParams).then(res => {
        if (res.code === 200) {
          this.dbTableList = res.rows
          this.total = res.total
        }
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList(this.selectTypeData[this.currentIndex], this.selectFormData[this.currentIndex])
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 导入按钮操作 */
    handleImportTable() {
      if (this.values.length === 0) {
        this.$modal.msgError("请选择要导入的信息")
        return
      }
      if (this.currentIndex === 0) {
        this.visible = false
        this.currentIndex = 1
        this.currentSelect = this.selectTypeData[this.currentIndex]
        this.values.forEach((item) => {
          // 建材名称
          this.materialName = item['equipmentName']
          // 单位
          this.unit = item['unit']
          this.carbonFactor = item['carbonFactor']
        })
        this.$modal.msg("您已选择设备，请继续选择运输方式")
        this.show()
        return
      }
      this.values.forEach((item) => {
        item['usagePhase'] = this.usagePhase
        item['equipmentType'] = this.equipmentType
        item['equipmentForm'] = this.equipmentForm
        item['engineeringId'] = this.engineeringId
        // 运输方式
        item['transportMode'] = item['equipmentName']
        item['transportFactor'] = item['carbonFactor']
        // 运输方式的因子单位
        item['factorUnit'] = item['unit']
        // 建材的因子单位
        item['unit'] = this.unit

        // 建材
        item['materialName'] = this.materialName
        item['carbonFactor'] = this.carbonFactor
      })
      addMasterialTransport(this.values[0]).then(res => {
        this.$modal.msgSuccess(res.msg)
        if (res.code === 200) {
          this.visible = false
          this.$emit("refreshMaterialInfo")
        }
      })
      this.currentIndex = 0
      this.currentSelect = this.selectTypeData[this.currentIndex]
    }
  }
}
</script>
